package cn.boysoft.core.client.json;



import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import cn.boysoft.core.model.message.Request;
import cn.boysoft.core.model.message.json.JSONRequest;
import cn.boysoft.core.util.JsonUtil;
import cn.boysoft.module.security.model.entity.UserEntity;

/**
 * 
 * @author 付厚俊
 * CreateTime 2013-4-4 下午5:09:32
 */

public class JsonClient {
	private String url="http://localhost:8080/jee_web/a.ajax";
	
	@SuppressWarnings("deprecation")
	public String send(String json) throws HttpException, IOException{
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		URI uri = new URI(url);
		PostMethod method = new PostMethod();
		method.setURI(uri);
		method.setRequestBody(json);
		httpClient.executeMethod(method);
		String response = method.getResponseBodyAsString();
		return response;
	}
	
	public static void main(String[] args) throws HttpException, IOException{
		long start = System.currentTimeMillis();
		for(int i=0;i<1;i++){
			HttpClient http = new HttpClient();
			URI uri = new URI("http://localhost:8080/jee_web/a.ajax");
			http.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			PostMethod method = new PostMethod();
			method.setURI(uri);
			UserEntity user = new UserEntity();
			
			user.setUserName("fuhoujun3");
			Request request = new JSONRequest();
			request.setData(user);
			request.setServiceId("01_02_02_03");
			String jsonStr = JsonUtil.toJson(request);
			method.setRequestBody(jsonStr);
			
			http.executeMethod(method);
			String response = method.getResponseBodyAsString();
			System.out.println(response);
		}
		
/*
			HttpClient http = new HttpClient();
			URI uri = new URI("http://localhost:9090");
			PostMethod method = new PostMethod();
			method.setURI(uri);
			
			Request request = new JSONRequest();
			request.setData("297e58333e0706bc013e0706f9d10000");
			request.setServiceId("002002");
			String jsonStr = JsonUtil.toJson(request);
			method.setRequestBody(jsonStr);
			
			http.executeMethod(method);
			String response = method.getResponseBodyAsString();
			System.out.println(response);
		*/
	}
	  
}
